package com.linq.cool.common.core.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @Author: yqlin
 * @Date: 2020/12/29 12:16
 * @Description: 响应信息主体
 * @Version: 1.0.0
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {

    /**
     * 成功
     */
    public static final Integer SUCCESS = HttpStatus.OK.value();

    /**
     * 失败
     */
    public static final Integer FAIL = HttpStatus.INTERNAL_SERVER_ERROR.value();

    private static final long serialVersionUID = 126679265726701049L;
    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;

    public static <T> Result<T> restResult(Object data, Integer code, String message) {
        return new Result<>(code, message, (T) data);
    }

    public static <T> Result<T> ok() {
        return restResult(null, Result.SUCCESS, HttpStatus.OK.getReasonPhrase());
    }

    public static <T> Result<T> ok(Object data) {
        return restResult(data, Result.SUCCESS, HttpStatus.OK.getReasonPhrase());
    }

    public static <T> Result<T> ok(String message) {
        return restResult(null, Result.SUCCESS, message);
    }

    public static <T> Result<T> ok(Object data, String message) {
        return restResult(data, Result.SUCCESS, message);
    }

    public static <T> Result<T> ok(Integer code, String message, Object data) {
        return restResult(data, code, message);
    }

    public static <T> Result<T> fail() {
        return restResult(null, Result.FAIL, HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    public static <T> Result<T> fail(String message) {
        return restResult(null, Result.FAIL, message);
    }

    public static <T> Result<T> fail(Object data) {
        return restResult(data, Result.FAIL, HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    public static <T> Result<T> fail(Object data, String message) {
        return restResult(data, Result.FAIL, message);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return restResult(null, code, message);
    }
}
